/**
 * COPYRIGHT HangZhou Trustway Technology Company Limited
 * All right reserved.
 */
package com.robam.cloud.authcenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.robam.cloud.authcenter.dao.AuthPlatformMapper;
import com.robam.cloud.authcenter.helper.ThirdAuthGateway;
import com.robam.cloud.authcenter.manager.ThirdAuthManager;
import com.robam.cloud.authcenter.service.AuthService;
import com.robam.cloud.common.bean.authcenter.entity.AuthPlatform;
import com.robam.cloud.common.bean.authcenter.enumspace.AuthPlatformTypeEnum;
import com.robam.cloud.common.bean.common.Constant;
import com.robam.cloud.common.bean.restful.entity.RespMsg;
import com.robam.cloud.common.bean.restful.entity.ServiceException;
import com.robam.cloud.common.bean.usercenter.entity.LoginAccount;
import com.robam.cloud.common.bean.usercenter.entity.UserSession;
import com.robam.cloud.common.util.TokenEncoder;
import com.robam.cloud.restful.config.basefeign.FeignUserCenterBaseUtil;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author 薛铁琪
 * @CreateTime 2020-07-20 15:22
 * @Version 1.0
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    AuthPlatformMapper authPlatformMapper;
    @Autowired
    ThirdAuthManager thirdAuthManager;
    @Autowired
    FeignUserCenterBaseUtil feignUserCenterBaseUtil;
    @Autowired
    TokenEncoder tokenEncoder;
    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public String getToken(String platformUuid, String code, String ticket, String dealKey, HttpServletRequest httpServletRequest) {
        AuthPlatform authPlatform = authPlatformMapper.selectByUuid(platformUuid);
        //登录登录类型路由登录工具
        AuthPlatformTypeEnum authPlatformTypeEnum = AuthPlatformTypeEnum.getAuthPlatformTypeEnumByCode(authPlatform.getPaltformType());
        if (authPlatform == null) {
            throw new ServiceException("登录平台uuid不正确请检查后重试");
        }
        ThirdAuthGateway thirdAuthGateway = thirdAuthManager.call(authPlatformTypeEnum);
        JSONObject request = new JSONObject();
        request.put("code", code);
        request.put("ticket", ticket);
        request.put("dealKey", dealKey);
        //登录工具获取user loginaccount
        UserSession userSession = thirdAuthGateway.getUserSession(authPlatform, request, httpServletRequest);
        //初始化user loginaccount
        RespMsg respMsg = feignUserCenterBaseUtil.initAccountUser(userSession);
        if (respMsg == null) {
            throw new ServiceException("用户中心初始化账号异常请稍后重试");
        }
        if (respMsg.getCode() != 200) {
            throw new ServiceException(respMsg.getMsg());
        }
        LoginAccount newAccount = (LoginAccount) respMsg.getData();
        if (newAccount == null) {
            throw new ServiceException("用户中心初始化账号异常请稍后重试");
        }
        String token = tokenEncoder.generateToken(newAccount.getId(), null);
        redisTemplate.opsForValue().set(Constant.TOKENNAME + ":" + token, newAccount.getId(), tokenEncoder.getExpireTime(), TimeUnit.MINUTES);
        if (StringUtils.isNullOrEmpty(token)) {
            throw new ServiceException("用户授权失败");
        }
        return token;
    }
}
